package top.huhuiyu.teachproject.aop;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import top.huhuiyu.teachproject.exception.AppException;
import top.huhuiyu.teachproject.service.AuthService;

/**
 * 权限管理切面
 *
 * @author 胡辉煜
 */
@Aspect
@Component
@Slf4j
@RequiredArgsConstructor
@Order(30)
public class ControllerAuth implements BaseControllerAop {
  private final AuthService authService;
  public static final int NEED_AUTH_CODE = 1000;
  public static final String NEED_AUTH_MESSAGE = "需要相关角色登录";

  @Before("controller()")
  public void before(JoinPoint pjp) {
    log.debug("权限切面处理");
    if (!authService.checkAuth()) {
      throw AppException.getInstance(NEED_AUTH_CODE, NEED_AUTH_MESSAGE);
    }
  }
}